package com.wantupai.mvp.framework.lce.impl.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;
import com.wantupai.mvp.mvp.lce.fragment.MvpFragment;


/**
 * @param <V>
 * @param <P>
 * @author zhangqing
 */
public abstract class BaseMvpFragment<D, V extends MvpView<D>, P extends MvpPresenter<V>> extends MvpFragment<D, V, P>
{
    private static final String TAG = "BaseMvpFragment";
    //我们自己的Fragment需要缓存视图
    private View viewContent;//缓存视图
    private boolean isInit;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        if (viewContent == null)
        {
            viewContent = inflater.inflate(getContentView(), container, false);
            initNavigation((ViewGroup) viewContent);
            initContentView(viewContent);
        }

        //判断Fragment对应的Activity是否存在这个视图
        ViewGroup parent = (ViewGroup) viewContent.getParent();
        if (parent != null)
        {
            //如果存在,那么我就干掉,重写添加,这样的方式我们就可以缓存视图
            parent.removeView(viewContent);
        }
        return viewContent;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (!isInit)
        {
            this.isInit = true;
            loadData();
        }
    }

    public abstract int getContentView();

    public void loadData()
    {
        loadData(false);
    }

    public void initContentView(View contentView)
    {

    }

    public void initNavigation(ViewGroup contentView)
    {

    }
}
